package com.hzjh.base.ext.widget.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import com.hzjh.base.ext.databinding.JhDialogAdFailedBinding
import com.hzjh.base.style.R

class JHReviewFailedDialog (context: Context?, themeResId: Int) : Dialog(context!!, themeResId) {

    class Builder(private val context: Context) {
        private var contentView: View? = null
        private var canCancel: Boolean = true
        private var canceledOnTouchOutside: Boolean = true
        private var heightOffSize: Float = 0.toFloat()
        private var withOffSize: Float = 0.toFloat()
        private var contentIsCenter: Boolean = true
        private var content:String = ""
        /**
         * 是否可以关闭dialog 如返回键 点外边
         * false 不能关闭
         * true 可以关闭 默认true
         * 这个优先级最高
         */
        fun setCanCancel(canCancel: Boolean): Builder {
            this.canCancel = canCancel
            return this
        }
        /**
         * 是否点击外部区域关闭dialog
         * false 不能关闭
         * true 可以关闭 默认true
         */
        fun setCanceledOnTouchOutside(canceledOnTouchOutside: Boolean): Builder {
            this.canceledOnTouchOutside = canceledOnTouchOutside
            return this
        }

        fun setContent(content:String):Builder{
            this.content=content
            return this
        }

        fun create(): JHAdSuccessDialog {
            /**
             * 初始化Dialog
             */
            val dialog = JHAdSuccessDialog(
                context,
                R.style.custom_dialog
            )

            /**
             * 初始化Dialog的布局页面
             */

            val vBinding: JhDialogAdFailedBinding =
                JhDialogAdFailedBinding.inflate(LayoutInflater.from(context), null, false)

            /**
             * 将初始化完整的布局添加到dialog中
             */
            dialog.setContentView(vBinding.root)

            if (canCancel) {
                /**
                 * 禁止点击Dialog以外的区域时Dialog消失
                 */
                dialog.setCanceledOnTouchOutside(canceledOnTouchOutside)
            } else {
                /**
                 * 禁用键盘搜索键
                 * https://blog.csdn.net/lk512893708/article/details/51997811
                 */
                dialog.setOnKeyListener { _, keyCode, _ -> keyCode == KeyEvent.KEYCODE_SEARCH }
                dialog.setCancelable(false)
            }

            val window = dialog.window
            val context = this.context as Activity
            val windowManager = context.windowManager

            val defaultDisplay = windowManager.defaultDisplay

            val attributes = window!!.attributes

            if (withOffSize.toDouble() != 0.0) {

                attributes.width = (defaultDisplay.width * withOffSize).toInt()
            } else {
                attributes.width = (defaultDisplay.width * 0.77).toInt()

            }
            if (heightOffSize.toDouble() != 0.0) {

                attributes.height = (defaultDisplay.height * heightOffSize).toInt()
            }
            window.attributes = attributes

            vBinding.tvContentDialog.text = content
            vBinding.ivCloseDialog.setOnClickListener {
                dialog.dismiss()
            }

            return dialog
        }
    }
}